/*
 * sme_test.c
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/fs.h>
#include "asm/cacheflush.h"


MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SME DECRYPT/ENCRYPT TEST");


#define SUCCESS 0
#define CACHE_SIZE  64
#define PAGE_SIZE  4096
#define SME_TEST_LEN  PAGE_SIZE * 2

int set_memory_encrypted(unsigned long addr, int numpages);
int set_memory_decrypted(unsigned long addr, int numpages);

int sme_test_init_module(void)
{
    unsigned char * buffer;
    unsigned char * start;

    printk("\n");
    printk("--------start sme_test--------\n");

    buffer = kmalloc(SME_TEST_LEN, GFP_KERNEL);
    start =(unsigned char*)(((unsigned long)buffer + PAGE_SIZE - 1) & 0xfffffffffffff000);
    memset(buffer, 0xab, SME_TEST_LEN);

    clflush(start);

    printk("original data: \n");
    printk("0x%x,0x%x,0x%x,0x%x\n", start[0],start[1],start[2],start[3]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[4],start[5],start[6],start[7]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[8],start[9],start[10],start[11]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[12],start[13],start[14],start[15]);

    set_memory_decrypted((unsigned long)start, CACHE_SIZE);

    printk("clear C bit, sme encrypted data: \n");
    printk("0x%x,0x%x,0x%x,0x%x\n", start[0],start[1],start[2],start[3]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[4],start[5],start[6],start[7]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[8],start[9],start[10],start[11]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[12],start[13],start[14],start[15]);

    set_memory_encrypted((unsigned long)start, CACHE_SIZE);

    printk("set C bit data, sme decrypted data: \n");
    printk("0x%x,0x%x,0x%x,0x%x\n", start[0],start[1],start[2],start[3]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[4],start[5],start[6],start[7]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[8],start[9],start[10],start[11]);
    printk("0x%x,0x%x,0x%x,0x%x\n", start[12],start[13],start[14],start[15]);

    printk("--------finish sme_test--------\n");
    printk("\n");
    kfree(buffer);

    return SUCCESS;
}

void sme_test_exit_module(void)
{

}


module_init(sme_test_init_module);
module_exit(sme_test_exit_module);
